<?php

abstract class BaseController {

    protected $_view;

    public function __construct() {
        $this->_view = new BaseView(new Request);
    }

    abstract public function index();

    protected function getLibrary($libreria) {
        $rutaLibreria = ROOT . 'libs' . DS . $libreria . '.php';

        if (is_readable($rutaLibreria)) {
            require_once $rutaLibreria;
        } else {
            throw new Exception('Error de libreria');
        }
    }

    protected function redirect_to($method = '', $args = array()) {
        if (empty($method) && empty($args)) {
            header('location:' . BASE_URL);
            exit;
        } else {
            header('location:' . $this->get_url($method, $args));
            exit;
        }
    }

    /**
     *  get_url(method_name[, arg1[, arg2...[, argN]]])
     * 
     *  return: BASE_URL/controller_name/method_name/arg1/arg2/.../argN
     * */
    protected function get_url($method, $args = array()) {
        $url_components = array_merge(array($this->name_for_url()), $method, $args);
        return BASE_URL . implode("/", $url_components);
    }

    function name_for_url() {
        return strtolower(substr(get_class($this), 0, strlen(get_class($this)) - strlen(SUFFIX_CLASS_CONTROLLER)));
    }

}

?>
